package com.example.demo.algorithm.lesson.basic.class3;

import java.util.Arrays;

/**
 * @author liweizhi
 * @date 2020/4/27
 */
public class Temp {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {4, 5, 6, 7};
        System.out.println(Arrays.toString(merge(arr1, arr2)));
    }

    public static int[] merge(int[] arr1, int[] arr2) {
        if (arr1 == null || arr1.length == 0 ) {
            return arr2;
        }
        if (arr2 == null || arr2.length == 0 ) {
            return arr1;
        }
        int[] ret = new int[arr1.length + arr2.length];
        int l1 = 0;
        int l2 = 0;
        int i = 0;
        while (l1 < arr1.length && l2 < arr2.length) {
            if (arr1[l1] <= arr2[l2]) {
                ret[i++] = arr1[l1++];
            } else {
                ret[i++] = arr2[l2++];
            }
        }

        while (l1 < arr1.length) {
            ret[i++] = arr1[l1++];
        }

        while (l2 < arr2.length) {
            ret[i++] = arr2[l2++];
        }

        return ret;
    }
}
